package lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 1
 * @version 1.0
 * @description: TODO
 * @date 2025-02-26 16:15
 */
public class Demo1 {
    private static int counter1 = 0;
    private static int counter2 = 0;
    private static int counter3 = 0;

    public static void main(String[] args) throws InterruptedException {
        test1();
        test2();
        test3();


    }

    /**
     * 不加锁, 可能出现线程不安全的情况,执行多次结果会小于预期值
     *
     */
    public static void test1() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(() -> {
                try {
                    //模拟业务场景执行
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                counter1++;
            });
            threads[i].start();
        }
        // 等待所有线程完成
        for (Thread thread : threads) {
            thread.join();
        }
        long end = System.currentTimeMillis();
        System.out.println("test1没有锁结果: counter = " + counter1+ "  耗时：" + (end - start)+" 毫秒");
        //counter = 98
    }
    /**
     * 加synchronized锁, 保证了线程安全
     *
     */
    public static void test2() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(() -> {
                synchronized (Demo1.class) {
                    //模拟业务场景执行
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    counter2++;
                }
            });
            threads[i].start();
        }
        // 等待所有线程完成
        for (Thread thread : threads) {
            thread.join();
        }
        long end = System.currentTimeMillis();
        System.out.println("test2有锁synchronized结果: counter = " + counter2+ "  耗时：" + (end - start) +" 毫秒");
    }

    /**
     * 加synchronized锁, 保证了线程安全
     *
     */
    public static void test3() throws InterruptedException {
        long start = System.currentTimeMillis();

        ReentrantLock lock = new ReentrantLock();
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(() -> {
                lock.lock();
                try {
                    //模拟业务场景执行
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    counter3++;

                }finally {
                    lock.unlock();
                }

            });
            threads[i].start();
        }
        // 等待所有线程完成
        for (Thread thread : threads) {
            thread.join();
        }
        long end = System.currentTimeMillis();
        System.out.println("test3有锁ReentrantLock结果: counter = " + counter3+ "  耗时：" + (end - start)+" 毫秒");    }



}
